package com.jiutong.meipengdai.ui.activitys;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;

import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.base.BaseActivity;
import com.jiutong.meipengdai.common.Constants;
import com.jiutong.meipengdai.entity.TinkerPatchRespond;
import com.jiutong.meipengdai.http.present.CommonPresent;
import com.jiutong.meipengdai.http.subscribers.SubscriberOnNextListener;
import com.jiutong.meipengdai.utils.DownLoadUtil;
import com.jiutong.meipengdai.utils.ExampleUtil;
import com.jiutong.meipengdai.utils.SpConfig;
import com.orhanobut.logger.Logger;

import java.util.HashMap;

/**
 * com.best.bestedu.activity
 * create for 启动页 判断是否进入引导页还是主页
 * Created by suxi  on 2017/6/19.
 */
public class IndexActivity extends BaseActivity {

    protected Handler mHandler = new Handler();
    /**
     * 是否退出APP后重新启动
     */
    private boolean isFirstLoading = false;

    @Override
    protected void initView() {
        super.initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        init();
    }

    /**
     * 判断是否第一次进入，是的话进入引导页。每次重新进入的时候启动页停留1.5S
     */
    private void init() {
        boolean isFirstUse = SpConfig.getInstance().getBool(
                Constants.IS_FIRST_USE_BOOLEAN, true);

        getPatch();
        if (isFirstUse) {
            openActivity(GuideActivity.class);
            IndexActivity.this.finish();

        } else {
            if (!isFirstLoading) {
                isFirstLoading = true;
                mHandler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        entryMain();
                    }
                }, 1500);
            }
        }
    }

    private void getPatch() {

        //检查系统是否开启了读写权限; 7.0以上的系统必须检查
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
            return;
        }

        final String versionName = ExampleUtil.getVersion(mContext);

//        查询补丁
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("versionName", versionName);

        CommonPresent.getPatch(mContext, new SubscriberOnNextListener<TinkerPatchRespond>() {
            @Override
            public void onNext(TinkerPatchRespond respond) {
                if ("200".equals(respond.getResCode()) && respond.getData() != null) {
                    String versionPatch = SpConfig.getInstance().getString(versionName);
                    String link = respond.getData().getLink();
//                    String link = "http://192.168.1.111:8080/patch.apk";
                    Logger.d("patch == " + link);
                    if (!TextUtils.isEmpty(link) && !link.equals(versionPatch)) {
                        new DownLoadUtil(mContext).downloadPatch(link);
                    }
                }

            }

            @Override
            public void onError(Throwable e) {

            }
        }, hashMap);
    }

    /**
     * 进入主界面
     */
    private void entryMain() {
        openActivity(MainActivity.class);
        IndexActivity.this.finish();
    }

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_index;
    }

}
